package com.shuyuntu.proposal.controller;

import com.shuyuntu.proposal.entity.Delegate;
import com.shuyuntu.proposal.service.DelegateService;
import com.shuyuntu.proposal.vo.LoginUserVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 登录界面的控制器
 * Created by limeng on 16/4/19.
 */
@Controller
@RequestMapping("/json")
public class JsonController {
    @ResponseBody
    @RequestMapping("/loginCheck")
    public Map<String, Object> loginCheck(@RequestBody LoginUserVO loginUserVO,
                                          HttpSession session) {
        Map<String, Object> resultMap = new LinkedHashMap<>();

        String userId = loginUserVO.getUserId();
        String userPwd = loginUserVO.getUserPwd();

        if (userId == null || userPwd == null) {
            resultMap.put("state", false);
            resultMap.put("result", "用户名或密码为空");

            return resultMap;
        }

        Delegate delegate = delegateService.getDelegateById(userId);
        if (delegate != null && delegate.getPassword().equals(userPwd)) {
            session.setAttribute("loginUserId",userId);
            resultMap.put("state", true);
        } else {
            resultMap.put("state", false);
            resultMap.put("result", "用户不存在或密码错误");
        }
        return resultMap;
    }

    @Resource
    private DelegateService delegateService;

    public DelegateService getDelegateService() {
        return delegateService;
    }

    public void setDelegateService(DelegateService delegateService) {
        this.delegateService = delegateService;
    }

}
